package lc.elensliu.ipcservice;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

import com.ipc.aidl.implement.Constants;

public class MessageService extends Service {


    private final Messenger messenger = new Messenger (new Handler () {

        @Override
        public void handleMessage (Message msg) {
            super.handleMessage (msg);
            int what = msg.what;
            switch (what) {

                case Constants.SEND_MSG:
                    Message message = Message.obtain (null,
                                                      Constants.GET_RESULT);


                    Bundle bundle = msg.getData ();
                    String content = bundle.getString (Constants.MSG_KEY);
                    bundle.putString (Constants.MSG_KEY,
                                      content + "_callback");
                    message.setData (bundle);
                    try {
                        msg.replyTo.send (message);

                    } catch (Exception e) {
                        e.printStackTrace ();
                    }
                    break;

                default:
                    break;
            }

        }
    });


    @Override
    public IBinder onBind (Intent intent) {

        Log.w ("MessageService",
               "onBind");
        return messenger.getBinder ();
    }
}
